package com.sdcqjy.property.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.TextView;

import com.sdcqjy.mylibrary.widget.photoView.RoundedImageView;
import com.sdcqjy.property.MyConstants;
import com.sdcqjy.property.R;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.utils.GlideUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/4/30.
 * 功能：
 */

public class MyZoneActivity extends SimpleBaseActivity {
    @BindView(R.id.icon)
    RoundedImageView icon;
    @BindView(R.id.textName)
    TextView textName;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (MyConstants.ActionLogin.equals(action)) {
                if (!intent.getBooleanExtra("login", false)) finish();
            }
        }
    };

    public static void open(Context context) {
        Intent intent = new Intent(context, MyZoneActivity.class);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myzone);
        ButterKnife.bind(this);

        IntentFilter intentFilter = new IntentFilter(MyConstants.ActionLogin);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, intentFilter);
    }

    protected void asynRun() {
    }

    protected void onResume() {
        super.onResume();
        if (isLogin(false)) {
            LoginMode mode = LoginMode.getMode(getActivity());
            textName.setText(mode.userName);
            GlideUtils.displayOfUrl(getActivity(), icon, mode.headImageUrl);
        } else {
            textName.setText("");
        }
    }

    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
    }

    @OnClick(R.id.layoutSelf)
    public void onLayoutSelfClicked() {
        if (isLogin()) SelfMsgActivity.open(getActivity(), icon, textName);
    }

    @OnClick(R.id.layoutAtten)
    public void onLayoutAttenClicked() {
        AttenListActivity.open(getActivity(), 0);
    }

    @OnClick(R.id.layoutRead)
    public void onLayoutReadClicked() {
        AttenListActivity.open(getActivity(), 1);
    }
}
